﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_HTTP_HOOKER_H
#define MK_HTTP_HOOKER_H
#include "MkHttpClientPool.h"
#include "MkUtil/MkSystemState.h"
#include "MkUtil/MkLock.h"
#include <json/json.h>

#ifdef MkUt
#define GetSystemStateSec 0  
#else
#define GetSystemStateSec 5  //mast more than 5 seconds
#endif

class MkApi CMkHttpHooker : public CMkHttpClientPool
{
public:
    CMkHttpHooker(CMkEventPool* pEventPool, CMkTimer* pTimer);
    virtual ~CMkHttpHooker();

    Uint32 StartSystemStateTimer();
    void SetSystemState(const CSystemState& State) { m_SystemState = State; }
    void SetLocalId(const MkString& Id) { m_LocalId = Id; }
    void SetHttpUrl(const MkString& Url) { m_HttpUrl = Url; }
    void SetUserPass(const MkString& UserName, const MkString& Password);
    void SetPayloadCount(Uint32 nCount) { m_PayloadCount = nCount; }
    void SetMapUriParam(const MkMapString& MapUriParam) { m_MapUriParam = MapUriParam; }
    void AddUriParam(const MkString& StrKey, const MkString& StrValue) { m_MapUriParam[StrKey] = StrValue; }

    const MkString& GetLocalIdId()const { return m_LocalId; }
    const MkString& GetHttpUrl() const { return m_HttpUrl; }
    const MkString& GetUserName() const { return m_UserName; }
    const MkString& GetPassword() const { return m_Password; }
    const MkMapString& GetUriParam() const { return m_MapUriParam; }
    Uint32 GetSystemInfo(Json::Value &JsonData);
    Uint32 NotifyKeepAlive();
    Uint32 NotifyMsg(const MkString &Action, const Json::Value &JsonData, const MkString &ServerId = "");
    Uint32 AsyncTransparentMessage(const Json::Value &Reqeust, const MkHttpResponseJsonCb& cb);
    Uint32 AsyncSessionActiveRequest(const MkString &Session, const MkHttpResponseJsonCb& cb);
    Uint32 AsyncMsg(const MkString &Action, const Json::Value &JsonData, const MkHttpResponseJsonCb& cb);

    Uint32 Release();
private:
    MkString                m_LocalId;               //local server id
    MkString                m_HttpUrl;
    MkString                m_UserName;
    MkString                m_Password;
    MkMapString             m_MapHeader;
    MkMapString             m_MapUriParam;         //http request header param

    Uint32                  m_PayloadCount;
    CSystemState            m_SystemState;
    MkString                m_GetSystemTimer;
};


#endif
